﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.BankSpecialAccount.Entitys;
using Modules.System.User;
using PetaPoco;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.DBContext;
using PmSoft.Entity;
using PmSoft.Repositories;
using PmSoft.Utilities;

namespace Modules.BankSpecialAccount.Repositories
{
    [UseDI(ServiceLifetime.Scoped)]
    public class BankSpecialAccountPaymentRepository : Repository<BankSpecialAccountPayment, DefaultDbContext>
    {
        private readonly ILogger logger;
        private readonly IApplicationContext applicationContext;

        public BankSpecialAccountPaymentRepository(DefaultDbContext dbContext, IApplicationContext applicationContext, ILogger<BankSpecialAccountPaymentRepository> logger)
           : base(dbContext, applicationContext)
        {
            this.logger = logger;
            this.applicationContext = applicationContext;
        }

        /// <summary>
        /// author:hzj date:2023/11/8 describe:获取收支信息数据集合(分页)
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<BankSpecialAccountPayment> GetProjectSpecialAccountPayments(BankSpecialAccountPaymentQuery query, int pageIndex, int pageSize)
        {
            Sql sql = Sql.Builder;
            sql.Select("id")
                .From("project_special_account_payment");

            #region 主管单位数据权限

            if (applicationContext.CurrentUser is IDeptUser deptUser && !deptUser.IsSuperAdministrator())
            {
                sql.InnerJoin("project as prj").On($"project_special_account_payment.project_id=prj.project_id and prj.is_deleted=0 and prj.industry_type=@0 ", deptUser.IndustryType);
                if (deptUser.Level == AdministrativeLevel.City)
                    sql.Append($" and prj.city_code={deptUser.CityCode}");
                if (deptUser.Level == AdministrativeLevel.Area)
                    sql.Append($" and prj.area_code={deptUser.AreaCode}");
            }

            #endregion 主管单位数据权限

            if (!string.IsNullOrEmpty(query.Keywords))
                sql.Where("serial_number like @0 or bank_serial_number like @0 or account_name like @0 or bank_no like @0 or  opposite_name like @0 or opposite_account like @0", "%" + StringUtility.StripSQLInjection(query.Keywords) + "%");
            if (query.ProjectId.HasValue)
                sql.Where("project_special_account_payment.project_id=@0", query.ProjectId.Value);
            if (query.AccountId.HasValue)
                sql.Where("account_id=@0", query.AccountId.Value);
            sql.OrderBy("project_special_account_payment.id DESC");
            return GetPagingEntities(pageSize, pageIndex, sql);
        }

        public IEnumerable<BankSpecialAccountPayment> GetProjectSpecialAccountPaymentsByProjectIds(IEnumerable<int> projectIds)
        {
            if (projectIds == null || projectIds.Count() == 0)
                return Enumerable.Empty<BankSpecialAccountPayment>();
            Sql sql = Sql.Builder;
            sql.Select("id")
                .From("project_special_account_payment");
            sql.Where("project_id in(@0)", projectIds);
            var ids = DbContext.FetchFirstColumn<int>(sql);
            return PopulateEntitiesByEntityIds(ids);
        }

        public bool ExistPayment(string serialNumber, string bankSerialNumber, string bankNo)
        {
            return DbContext.Exists<BankSpecialAccountPayment>("serial_number =@0 and bank_serial_number=@1 and bank_no=@2", serialNumber, bankSerialNumber, bankNo);
        }
    }
}